package com.cy.config;

import com.alibaba.druid.wall.WallFilter;
import com.cy.BasicController;
import com.cy.BasicModel;
import com.cy.ext.plugin.route.AutoBindRoutes;
import com.cy.ext.plugin.tablebind.AutoTableBindPlugin;
import com.cy.ext.plugin.tablebind.SimpleNameStyles;
import com.cy.interceptor.EventLogInterceptor;
import com.cy.utils.PropertyUtil;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class FrameConfig extends JFinalConfig {
    Routes routes;

    /**
     * 一般设置
     *
     * @param me
     */
    public void configConstant(Constants me) {
        me.setDevMode(getKernelConfig("devMode").equals("true"));
        me.setEncoding(getKernelConfig("encoding"));
        me.setBaseViewPath(getKernelConfig("baseViewPath"));
        me.setViewType(ViewType.JSP);
        me.setUploadedFileSaveDirectory(PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "tempDir"));
        me.setMaxPostSize(Integer.valueOf(PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "max_size")));
        configQiNiu();
        configSms();
    }

    private void configSms() {
//        SMSKit.setSmsService(new LuosimaoSMS());
//        SMSKit.setSmsService(new HXSMSServiceImpl("101100-WEB-HUAX-482716", "ZQVJFEEX"));
    }

    /**
     * 配置七牛云和上传的控件
     */
    public void configQiNiu() {
        Config.QiNiu.ACCESS_KEY = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "ACCESS_KEY");
        Config.QiNiu.SECRET_KEY = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "SECRET_KEY");
        Config.QiNiu.PUBLIC_BUCKET_NAME = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "PUBLIC_BUCKET_NAME");
        Config.QiNiu.AVATAR_BUCKET_NAME = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "AVATAR_BUCKET_NAME");
        Config.QiNiu.PUBLIC_BUCKET_URL = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "PUBLIC_BUCKET_URL");
        Config.QiNiu.AVATAR_BUCKET_URL = PropertyUtil.getGrosseValue(PropertyUtil.QINIU, "AVATAR_BUCKET_URL");
    }

    /**
     * 配置Controller
     *
     * @param me
     */
    public void configRoute(Routes me) {
        this.routes = me;
        me.add(new AutoBindRoutes());
    }

    /**
     * 配置插件、数据库
     *
     * @param me
     */
    public void configPlugin(Plugins me) {
        // 连接池插件
        DruidPlugin druidPlugin = DruidPluginConfig.getDruidPlugin();
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        wallFilter.setLogViolation(true);
        wallFilter.setThrowException(false);
        druidPlugin.addFilter(wallFilter);
        me.add(druidPlugin);

        // 缓存插件
        me.add(new EhCachePlugin());

        // 自动扫描绑定表的实体类,自动扫描继承了Model的
        AutoTableBindPlugin arp = new AutoTableBindPlugin(druidPlugin, SimpleNameStyles.GROSSE);
        arp.setShowSql(PropertyUtil.getGrosseValue(PropertyUtil.DB, "show_sql").equals("true"));
        arp.addExcludeClasses(BasicModel.class);
        me.add(arp);

        // 添加Quartz定时任务
//        me.add(new QuartzPlugin());
    }

    /**
     * 配置拦截器
     *
     * @param me
     */
    public void configInterceptor(Interceptors me) {
        me.add(new SessionInViewInterceptor(false));
        me.add(new EventLogInterceptor());
    }

    /**
     * 配置Handler
     *
     * @param me
     */
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("gPath"));
    }

    private String getKernelConfig(String key) {
        return PropertyUtil.getGrosseValue(PropertyUtil.KERNEL, key);
    }

    @Override
    public void afterJFinalStart() {
        BasicModel.initLogger();
        BasicController.initLogger();
    }
}
